package org.melanesia.beans;

import java.lang.reflect.Field;

import org.melanesia.beans.exceptions.PropertyAccessException;

/**
 * Property getter using field access.
 *
 * @author marcin.kielar
 */
class FieldGetter implements Getter {

    /** Reflection field for accessing the property. */
    private final Field field;

    /**
     * Constructs new <code>FieldGetter</code> with field accessing the
     * property.
     *
     * @param field
     *            field accessing the property
     */
    FieldGetter(final Field field) {
        this.field = field;
    }

    @Override
    public Object get(final Object bean) {

        try {
            return field.get(bean);
        } catch (Throwable t) {
            throw new PropertyAccessException("Cannot set value of field " + field.getName(), t);
        }
    }

    @Override
    public Class<?> getReturnType() {
        return field.getType();
    }

}
